#ifndef MOE_TOURNAMENT_SELECTION
#define MOE_TOURNAMENT_SELECTION

#include "engine/Selection.hh"


class TournamentSelection : public Selection {

public:
    TournamentSelection() : Selection() {}

    virtual ~TournamentSelection() {}

    Solution *select(Population *population) {

        int indexRed = Random::getInstance()->getInt(population->length());
        int indexBlue = Random::getInstance()->getInt(population->length());

        Solution *solutionRed = population->get(indexRed);
        Solution *solutionBlue = population->get(indexBlue);

        Solution *selected = solutionRed;

        if (solutionRed->dominates(solutionBlue)) selected = solutionRed;

        else if (solutionBlue->dominates(solutionRed)) selected = solutionBlue;

        else if (solutionRed->getCrowding() > solutionBlue->getCrowding()) selected = solutionRed;

        else if (solutionBlue->getCrowding() > solutionRed->getCrowding()) selected = solutionBlue;

        return selected->copy();

    }

};

#endif
